package com.mingtang.online.fragment;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mingtang.online.R;
import com.mingtang.online.view.IWebView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2015/12/11.
 */
public class FuLiFragment extends Fragment {

    @BindView(R.id.v4_webview)
    IWebView v4Webview;
    Unbinder unbinder;

    WebChromeClient chromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            Log.e("进度", newProgress + "");

            super.onProgressChanged(view, newProgress);

        }

        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            Log.e("console", "[" + consoleMessage.messageLevel() + "] " + consoleMessage.message() + "(" + consoleMessage.sourceId() + ":" + consoleMessage.lineNumber() + ")");
            return super.onConsoleMessage(consoleMessage);
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fuli, null);


        unbinder = ButterKnife.bind(this, view);
        initWebView();
        v4Webview.loadUrl("http://www.50210.com.cn/app/index.php?i=3&c=entry&type=11&do=newcat&m=tiger_newhu");
        return view;
    }

    public void initWebView() {
        v4Webview.clearCache(true);

        v4Webview.setWebChromeClient(chromeClient);

        v4Webview.getSettings().setUseWideViewPort(true);

        v4Webview.getSettings().setLoadWithOverviewMode(true);
        v4Webview.getSettings().setSavePassword(true);
        v4Webview.getSettings().setSaveFormData(true);
        v4Webview.getSettings().setJavaScriptEnabled(true);

        // enable navigator.geolocation
        v4Webview.getSettings().setGeolocationEnabled(true);
        v4Webview.getSettings().setGeolocationDatabasePath(
                "/data/data/org.itri.html5webview/databases/");

        v4Webview.requestFocus();
        v4Webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        // v4_webview.setScrollBarStyle(0);
        String ua = v4Webview.getSettings().getUserAgentString();
        v4Webview.getSettings().setUserAgentString(
                ua + "; " + "rmbbox_android");

        v4Webview.getSettings().setBuiltInZoomControls(true);
        v4Webview.getSettings().setLayoutAlgorithm(
                WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        v4Webview.getSettings().setDomStorageEnabled(true);

        v4Webview.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);

        v4Webview.getSettings().setAllowFileAccess(true);
        v4Webview.getSettings().setAppCacheEnabled(true);
        v4Webview.getSettings().setJavaScriptEnabled(true);
//        v4Webview.getSettings().setAppCachePath(appCachePath);
        v4Webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        v4Webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//		v4_webview.getSettings().setSupportZoom(true);
        v4Webview.getSettings().setDefaultTextEncodingName("utf-8");

        v4Webview.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent ev) {

                ((WebView) v).requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });
        v4Webview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {


                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub

                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub

                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
//                v4Webview.reload();
            }


        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
